package com.cy.milktea.controller;


import com.cy.milktea.pojo.Drink;
import com.cy.milktea.pojo.System.JsonResult;
import com.cy.milktea.service.DrinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/drink")
public class DrinkController {

    @Autowired
    public DrinkService drinkService;

    @PostMapping
    public JsonResult add(Drink drink) throws IOException {
        drinkService.add(drink);
        return JsonResult.ok("添加成功!");
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Integer id) {
        return drinkService.delete(id) == 1 ? JsonResult.ok("删除成功!") : JsonResult.error("id不存在!");
    }

    @PutMapping
    public JsonResult update(Drink drink) {
        drinkService.update(drink);
        return JsonResult.ok("修改成功!");
    }

    @GetMapping
    public JsonResult query(String name, Integer status) {
        if(name != null) name = "".equals(name.trim()) ? null : name.trim();
        status = status == -1 ? null : status;
        return JsonResult.ok("查询成功!", drinkService.query(name, status));
    }


}
